package experiment3.shudu001.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
import experiment3.shudu001.R;
import experiment3.shudu001.models.PrefsManager;

public class CompletionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_completion);

        // 获取传递的参数
        boolean isSuccess = getIntent().getBooleanExtra("isSuccess", true);
        long timeMillis = getIntent().getLongExtra("time", 0);
        int difficulty = getIntent().getIntExtra("difficulty", 1);
        int mistakes = getIntent().getIntExtra("mistakes", 0);

        // 初始化视图
        TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
        TextView timeTextView = (TextView) findViewById(R.id.timeTextView);
        TextView difficultyTextView = (TextView) findViewById(R.id.difficultyTextView);
        TextView mistakesTextView = (TextView) findViewById(R.id.mistakesTextViewCompletion);
        Button newGameButton = (Button) findViewById(R.id.newGameButtonCompletion);
        Button mainMenuButton = (Button) findViewById(R.id.mainMenuButtonCompletion);

        // 根据结果设置界面
        if (isSuccess) {
            // 成功时更新统计
            PrefsManager.updateStats(this, difficulty, timeMillis, mistakes);
            titleTextView.setText("恭喜完成!");
            titleTextView.setTextColor(getResources().getColor(R.color.success));
        } else {
            titleTextView.setText("游戏失败!");
            titleTextView.setTextColor(getResources().getColor(R.color.failure));
            timeTextView.setVisibility(View.GONE);
            difficultyTextView.setVisibility(View.GONE);
        }

        // 设置通用信息
        String timeFormatted = formatTime(timeMillis);
        String difficultyStr = getDifficultyString(difficulty);

        timeTextView.setText("时间: " + timeFormatted);
        difficultyTextView.setText("难度: " + difficultyStr);
        mistakesTextView.setText("错误次数: " + mistakes);

        // 按钮事件
        newGameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restartGame();
            }
        });

        mainMenuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                returnToMainMenu();
            }
        });
    }

    private String formatTime(long millis) {
        long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) % 60;
        return String.format("%02d:%02d", minutes, seconds);
    }

    private String getDifficultyString(int difficulty) {
        switch (difficulty) {
            case 1:
                return "简单";
            case 2:
                return "中等";
            case 3:
                return "困难";
            default:
                return "未知";
        }
    }

    private void restartGame() {
        Intent intent = new Intent(this, DifficultyActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    private void returnToMainMenu() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }
}